<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${tableEntity.pakagePre}.${tableEntity.entityName}Mapper">

    <#--<resultMap id="BaseResultMap">-->
    <#--<#list tableEntity.columFields as columField>-->
<#--<#if tableEntity.primaryKey==columField.columName?upper_case>-->
    <#--<id column="${columField.columName}" jdbcType="${columField.columType}" property="${columField.columName_l}" />-->
    <#--<#else >-->
        <#--<result column="${columField.columName}" jdbcType="${columField.columType}" property="${columField.columName_l}" />-->
    <#--</#if>-->
<#--</#list>-->
    <#--</resultMap>-->
        <!--全部列-->
    <sql id="Base_Column_List">
    <#list tableEntity.columFields as columField>
        ${columField.columName} as ${columField.filedName}<#if columField_has_next>,</#if>
    </#list>
    </sql>

        <!--单条插入-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="${tableEntity.entityName}" parameterType="java.util.Map">
        INSERT INTO ${tableEntity.dbName}.${tableEntity.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list tableEntity.columFields as columField>
            <if test="${columField.filedName}!=null">`${columField.columName}`,</if>
        </#list>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list tableEntity.columFields as columField>
            <if test="${columField.filedName}!=null">${r'#{'}${columField.filedName}},</if>
        </#list>
        </trim>
    </insert>

        <!--批量插入-->
    <insert id="insertList">
        INSERT INTO ${tableEntity.dbName}.${tableEntity.tableName}(
        <include refid="Base_Column_List"/>
        )VALUES
        <foreach collection="pojos" item="pojo" index="index" separator=",">
            (
        <#list tableEntity.columFields as columField>
            ${r'#{pojo.'}${columField.filedName}<#if columField_has_next>},</#if>
        </#list>
            )
        </foreach>
    </insert>

        <!--更新-->
    <update id="update" parameterType="java.util.Map">
        UPDATE ${tableEntity.dbName}.${tableEntity.tableName}
        <set>
        <#list tableEntity.columFields as columField>
            <if test="${columField.filedName} != null"> ${columField.columName} = ${r'#{'}${columField.filedName}}<#if columField_has_next>,</#if></if>
        </#list>
        </set>
        WHERE ${tableEntity.primaryKey} = ${r'#{'}${tableEntity.entityKey}}
    </update>

        <!--删除-->
    <delete id="delete">
        DELETE FROM ${tableEntity.dbName}.${tableEntity.tableName} WHERE ${tableEntity.primaryKey} = ${r'#{'}${tableEntity.entityKey}}
    </delete>

        <!--列表查询-->
    <select id="findList" resultType="dto" parameterType="java.util.Map">
        select
        <include refid="Base_Column_List"/>
        from ${tableEntity.dbName}.${tableEntity.tableName}
        <where>
        <#list tableEntity.columFields as columField>
            <if test="${columField.filedName} != null">AND `${columField.columName}` = ${r'#{'}${columField.filedName}}</if>
        </#list>
        </where>
    </select>

        <!--分页查询-->
    <select id="findlistPage" parameterType="page" resultType="dto">
        select
        <include refid="Base_Column_List"/>
        from ${tableEntity.dbName}.${tableEntity.tableName}
        <where>
        <#list tableEntity.columFields as columField>
            <if test="${r'#{dto.'}${columField.filedName} != null">AND `${columField.columName}` = ${r'#{dto.'}${columField.filedName}}</if>
        </#list>
        </where>
        ORDER BY
        ${tableEntity.primaryKey} DESC
    </select>

</mapper>